<template>
  <div>
    <!-- 列表 -->
    <!-- 
          data:数据
          row-key：一行的唯一标识
          border：加边框
           default-expand-all:默认展开所有数据
          default-expand-all：默认展开所有数据
           :tree-props="{ children: '由哪个字段来判断是否有子节点' }"
    -->

    <el-table
      :data="list"
      style="width: 100%;"
      row-key="id"
      border
      :tree-props="{children: 'children'}"
    >
      <el-table-column prop="id" label="规格编号" sortable width="180"></el-table-column>
      <el-table-column prop="specsname" label="规格名称" sortable width="180"></el-table-column>
      <el-table-column label="规格属性" sortable width="180">
        <template slot-scope="scope">
          <el-tag v-for="(item,index) in scope.row.attrs" :key="index">{{item}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.status==1" @click="handleEdit(scope)">启用</el-button>
          <el-button type="info" v-else>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope)">编辑</el-button>
          <!-- <el-button type="danger" @click="handleDelete(scope)">删除</el-button> -->
          <!-- 绑定 comfirm -->
          <del-btn @comfirm="del(scope)"></del-btn>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <!-- 总数：:total="50"   一页的数量：:page-size="3"  绑定事件 @change=changePage-->
    <el-pagination background layout="prev, pager, next" :total="total" :page-size="size" @current-change=changePage></el-pagination>
  </div>
</template>

<script>
import { reqspecsdelete } from "../../../http/api";
import { successAlert } from "../../../utils/alert";

// 8.3 引入辅助函数
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      list: "specs/list",
      //  1.取出总数，
      total: "specs/total",
      // 取出一页的数量
      size: "specs/size"
    })
  },
  methods: {
    ...mapActions({
      // 请列表 + 请总数
      reqList: "specs/reqList",
      changePage:'specs/changePage'
    }),
    handleEdit(scope) {
      //   console.log(scope);
      //   scope.$index 下标
      // scope.row 那条数据 item
      // 通知父组件,并且将这行的id带上
      this.$emit("willUpdate", scope.row.id);
    },

    //封装删除组件
    del(scope) {
      // console.log(scope.row);
      // 发删除请求
      reqspecsdelete({
        id: scope.row.id
      }).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //  重新获取list
          this.reqList();
        }
      });
    },
  },
  mounted() {
    // 8.4 一进页面就发请求
    this.reqList();
    // this.reqTotal()
  }
};
</script>

<style scoped>
img {
  width: 80px;
  height: 80px;
}
</style>